
######################################
# target
######################################
TARGET = app

######################################
# source
######################################
# C sources
C_SOURCES += \
$(wildcard gd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_standard_peripheral/Source/*.c)\
$(wildcard gd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Template/*.c)\
$(wildcard usr/src/*.c)\
$(wildcard usr/board_driver/*.c)\
gd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/CMSIS/GD/GD32F30x/Source/system_gd32f30x.c
#JlinkRTT
C_SOURCES += \
segger/Syscalls/SEGGER_RTT_Syscalls_GCC.c\
$(wildcard segger/RTT/*.c) 
#USB_CDC
C_SOURCES += \
gd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_usbd_library/class/device/cdc/Source/cdc_acm_core.c\
$(wildcard gd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_usbd_library/device/Source/*.c)\
$(wildcard gd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_usbd_library/usbd/Source/*.c)\
$(wildcard usr/cdc_acm/src/*.c)

# ASM sources
ASM_SOURCES =  \
gd32f303/GCC/startup_gd32f30x_hd.s
# ASM_SOURCES_S = \
# segger/RTT/SEGGER_RTT_ASM_ARMv7M.S
# C includes
C_INCLUDES =  \
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/CMSIS\
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/CMSIS/GD/GD32F30x/Include\
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_standard_peripheral/Include\
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Template
C_INCLUDES +=\
-Isegger/RTT\
-Iusr/src\
-Iusr/board_driver\
-Iusr/icon
C_INCLUDES +=\
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_usbd_library/usbd/Include\
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_usbd_library/device/Include\
-Igd32f303/GD32F30x_Firmware_Library_V3.0.1/GD32F30x_Firmware_Library_V3.0.1/Firmware/GD32F30x_usbd_library/class/device/cdc/Include\
-Iusr/cdc_acm/inc
# link script
LDSCRIPT = gd32f303/GCC/gd32f30x_flash.ld


######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization
OPT = -Og


#######################################
# paths
#######################################

# Build path
BUILD_DIR = build


#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4

# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)

# macros for gcc
# AS defines
AS_DEFS = 

# C defines
C_DEFS =  \
-DUSE_STDPERIPH_DRIVER \
-DGD32F30X_HD

# AS includes
AS_INCLUDES = 

# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -Wno-maybe-uninitialized -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif


# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"


#######################################
# LDFLAGS
#######################################


# libraries
LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -u_printf_float -specs=nosys.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -Wl,--print-memory-usage

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin


#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))



$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
	
$(BUILD_DIR):
	mkdir $@		

#######################################
# clean up
#######################################
clean:
	rm -rd $(BUILD_DIR)
  
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
